﻿

// Application/example code

var qtLoader = undefined;
var qtWindows = [];

//测试数组传递
function testFloatArray() {
    var length = 50;
    // Create example data to test float_multiply_array
    var data = new Float32Array(length);
    data[5] = 10.123;

    // Import function from Emscripten generated file
    setData = Module.cwrap(
        'setData', 'number', ['string', 'number', 'number']
    );

    // Get data byte size, allocate memory on Emscripten heap, and get pointer
    var nDataBytes = data.length * data.BYTES_PER_ELEMENT;
    //Module.TOTAL_MEMORY = nDataBytes; // added by jo
    var dataPtr = Module._malloc(nDataBytes);

    // Copy data to Emscripten heap (directly accessed from Module.HEAPU8)
    var dataHeap = new Uint8Array(Module.HEAPU8.buffer, dataPtr, nDataBytes);
    dataHeap.set(new Uint8Array(data.buffer));

    // Call function and get result
    setData("sadad", dataHeap.byteOffset, length);
    var t1bis = performance.now();
    var result = new Float32Array(dataHeap.buffer, dataHeap.byteOffset, data.length);

    // Free memory
    Module._free(dataHeap.byteOffset);
}

function initEnviroment(parmary) {
    const {success, error} = parmary;
    if (typeof (qtloader) == "undefined") {
        qtWindows = [];
        windowIndex = 2;


        // load Qt
        qtloader = QtLoader({
            canvasElements: qtWindows.map(function (canvas) {
                return canvas
            }
            ),
            showError: function (errorText) {
                console.log(errorText);
                if(error) error(errorText)
            },
            showCanvas: function () {
                qtWindows.map(function (canvas) { canvas.visible = true });
                //屏蔽右键菜单
                window.document.oncontextmenu = function () { return false };
                if(success) success()
            },
            statusChanged: status=>{
                console.log("status:",status)
            },
            path: '../utilJs'
        });
        qtloader.loadEmscriptenModule("multicanvas");
    } else {
        console.log('Enviroment is already init.');
    }
}

function checkEnviroment() {
    return typeof (qtloader) != "undefined";
}

function createLotWin(obj) {
    if (!checkEnviroment()) {
        return false;
    }

    // 自定义接收参数
    const { dom, type, name } = obj;

    canvasObj = document.createElement("canvas");
    canvasObj.id = type + name
    canvasObj.style.width = "100%";
    canvasObj.style.height = "100%";
    canvasObj.oncontextmenu = "event.preventDefault()";

    dom.appendChild(canvasObj);
    qtWindows.push(canvasObj);

    //该函数触发addScreenWindow事件
    qtloader.addCanvasElement(canvasObj);

    return true;
}

function initLot(obj) {
    if( Object.prototype.toString.call(obj) == '[object Object]'){
        createLotWin(obj);
    }
    if( Object.prototype.toString.call(obj) == '[object Array]'){
        for(let i in obj) {
            createLotWin(obj[i])
        }
        // obj.forEach(item => {
        //     createLotWin(item)
        // })
    }
}

function test() {
    createLotWin({
        dom: document.getElementById("plot_container"),
        type: "mesh",//validate: ['time', 'spectum', 'waterfall',  'mesh'];
        name: "time2"
    });

}
